﻿
using System;
using System.Xml.Serialization;

namespace QLab.Controllers.SolutionExplorer
{

	[XmlRootAttribute(ElementName = "Solution", Namespace = "", IsNullable = false)]
	public class SolutionExplorerItemSolution : SolutionExplorerItemBase
	{
		protected string _solutionRootPath = String.Empty;

		public SolutionExplorerItemSolution()
			: base(SolutionExplorerItemType.Solution)
		{

#if DEBUG

			#region KeyOPS Test
			//// KeyOPS Test
			//ChildItems.Add(new SolutionExplorerItemProject() { Path = @"C:\Temp\QLib\TestProject_01" });
			//ChildItems.Add(new SolutionExplorerItemProject() { Path = @"C:\Temp\QLib\TestProject_02" });
			//ChildItems.Add(new SolutionExplorerItemProject() { Path = @"C:\Temp\QLib\TestProject_03" });
			#endregion

#endif

		}

		/*
		public SolutionExplorerItemSolution(string SolutionFullPath)
		  : base(SolutionExplorerItemType.Solution)
		{
		  _solutionRootPath =System.IO.Path.GetDirectoryName(SolutionFullPath);
		  Name = System.IO.Path.GetFileName(SolutionFullPath);
		  //FullPath = String.Format("{0}.qlsln", System.IO.Path.Combine(SolutionPath, SolutionName));
		}
		*/

		public SolutionExplorerItemSolution(string SolutionPath, string SolutionName, string ProjectName)
			: base(SolutionExplorerItemType.Solution)
		{
			_solutionRootPath = SolutionPath;
			Name = System.IO.Path.GetFileNameWithoutExtension(SolutionName) + DefaultFileExtension;

			SolutionExplorerItemProject project =
			  new SolutionExplorerItemProject(SolutionPath, ProjectName);
			project.Parent = this;

			ChildItems.Add(project);
			project.Save();
			Save();
		}

		[XmlIgnoreAttribute()]
		public string SolutionRootPath
		{
			get { return _solutionRootPath; }
			set
			{
				_solutionRootPath = value;
				UpdateParentNodes();
			}
		}

		public override void Load(string SolutionPath)
		{
			/*
			SolutionExplorerItemSolution solution;

			XmlSerializer solutionSerializer = new XmlSerializer(typeof(SolutionExplorerItemSolution));
			System.IO.FileStream solutionFileStream = new System.IO.FileStream(SolutionPath, System.IO.FileMode.Open);
			solution = (SolutionExplorerItemSolution)solutionSerializer.Deserialize(solutionFileStream);
			solutionFileStream.Close();
			*/
		}

		public override void Save()
		{
			System.IO.StreamWriter solutionStreamWriter = System.IO.File.CreateText(FullPath);
			XmlSerializer solutionSerializer = new XmlSerializer(typeof(SolutionExplorerItemSolution));
			solutionSerializer.Serialize(solutionStreamWriter, this);
			solutionStreamWriter.Close();

			foreach (SolutionExplorerItemBase item in ChildItems)
				((SolutionExplorerItemProject)item).Save();
		}

	}
}
